package com.thomas.framework.controller.admin;

import com.thomas.framework.core.annotation.Function;
import com.thomas.framework.core.annotation.FunctionMenu;
import com.thomas.framework.entity.SysModel;
import com.thomas.framework.service.ISysModelService;
import com.thomas.framework.utils.JsonUtil;
import com.thomas.framework.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * SysModel Controller
 * Created by taohj on 2014/4/23.
 */

@FunctionMenu("SysModelManagement")
@Controller
@RequestMapping("admin/sys/model/")
public class SysModelController {


    /**
     * 服务层模块辅助对象
     */
    @Autowired
    private ISysModelService service;

    /**
     * 模块新增页面
     */
    @Function("SysModelCreateView")
    @RequestMapping("createView")
    public String createView(HttpServletRequest request,@RequestParam(value = "parentId",required = false) String parentId ) {
        if (StringUtils.hasLength(parentId)){
            SysModel model=service.getById(new SysModel(parentId));
            request.setAttribute("parent",model);
        }
        return "admin/sys/model/create";
    }


    /**
     * 模块新增
     */

    @Function("SysModelCreate")
    @ResponseBody
    @RequestMapping("create")
    public String create(
            @RequestParam(value = "text", required = false) String text,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "isLeaf", required = false) String isLeaf,
            @RequestParam(value = "parentId", required = false) String parentId,
            @RequestParam(value = "sort", required = false) Integer sort,
            @RequestParam(value = "description", required = false) String description
    ) {
        PageResult pageResult = new PageResult();
        SysModel model = new SysModel();

        model.setText(text);
        model.setUrl(url);
        model.setType(type);
        model.setIsLeaf(isLeaf);
        model.setParent(new SysModel().setId((parentId==null || "".equals(parentId))?"root":parentId));

        model.setSort(sort);
        model.setDescription(description);
        pageResult.setSuccess(service.create(model));
        return pageResult.toString();
    }


    /**
     * 模块删除
     */
    @Function("SysModelRemove")
    @ResponseBody()
    @RequestMapping("remove")
    public String remove(@RequestParam("id") String id) {
        PageResult pageResult = new PageResult();
        SysModel model = new SysModel();
        model.setId(id);
        pageResult.setSuccess(service.remove(model));
        return  pageResult.toString();
    }




    /**
     * 模块修改页面
     * */


    @Function("SysModelModifyView")
    @RequestMapping("modifyView")
    public  String  modifyView(
            HttpServletRequest request,
            @RequestParam(value = "id",required = false) String id
    ){
        SysModel model=service.getById(new SysModel(id));
        request.setAttribute("model",model);
        return  "admin/sys/model/modify";
    }



    /**
     * 模块修改
     * */

    @Function("SysModelModify")
    @ResponseBody
    @RequestMapping("modify")
    public  String  modify(
            @RequestParam(value = "id",required = true) String id,
            @RequestParam(value = "text", required = false) String text,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "isLeaf", required = false) String isLeaf,
            @RequestParam(value = "parentId", required = false) String parentId,
            @RequestParam(value = "sort", required = false) Integer sort,
            @RequestParam(value = "description", required = false) String description
    ){
        PageResult pageResult=new PageResult();
        SysModel model = new SysModel();
        model.setId(id);
        model.setText(text);
        model.setUrl(url);
        model.setType(type);
        model.setIsLeaf(isLeaf);
        if (StringUtils.hasLength(parentId)) {
            model.setParent(new SysModel().setId(parentId));
        }
        model.setSort(sort);
        model.setDescription(description);
        pageResult.setSuccess(service.modify(model));
        return  pageResult.toString();
    }






    /**
     * 模块列表页面
     */
    @Function("SysModelManagement")
    @RequestMapping("management")
    public String listView() {
        return "admin/sys/model/management";
    }


    /**
     * 模块列表数据
     */
    @Function("SysModelList")
    @ResponseBody
    @RequestMapping("list")
    public String list(
            @RequestParam(value = "id", required = false) String parentId
    ) {
        SysModel  model=new SysModel((parentId==null || "".equals(parentId))?"root":parentId);

        List<SysModel>list= service.getChrildren(model);
        return JsonUtil.toString(list);
    }
}
